package com.int_tech.timeTask194;

import com.int_tech.MessageCodeAll.dao.CodeA4Dao;
import com.int_tech.MessageCodeAll.entity.CodeA4;
import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import com.int_tech.messionLog.dao.MessionLogDao;
import com.int_tech.messionLog.entity.MessionLog;
import com.int_tech.utils.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/6/22.
 */
@Component
public class CheckA4TimeTask {

    @Autowired
    private SocketLogSendDao socketLogSendDao;

    @Autowired
    private CodeA4Dao codeA4Dao;

    @Autowired
    private MessionLogDao messionLogDao;

    @Scheduled(cron="0/30 * * * * ?")
    public void checkA4() throws ParseException {

        PageRequest pageRequest = new PageRequest(0, 100);
//        Page<SocketLogSend> socketLogSendListPage = socketLogSendDao.findByCodeAndCheckStatusAndCarNumber(pageRequest,"A4",8080,"010AEA056");
        String day1 = DateTool.Date2String(new Date(),"yyyy-MM-dd");

        String date = day1 + " 00:00:00";

        Date onDate = DateTool.string2Date(date,"yyyy-MM-dd HH:mm:ss");
        Page<SocketLogSend> socketLogSendListPage = socketLogSendDao.findByCodeAndCheckStatusAndCreateTimeAfterOrderByCreateTimeAsc(pageRequest,"A4",0,onDate);
        List<SocketLogSend> socketLogSendList = socketLogSendListPage.getContent();


        //删除之前的
//        List<MessionLog> messionLogsBefore = messionLogDao.findAll();

        for(SocketLogSend socketLogSend : socketLogSendList)
        {
            try
            {
                socketLogSend.setCheckStatus(1);
                socketLogSendDao.save(socketLogSend);

                String logData = socketLogSend.getDataString();

                String[] logs = logData.split(",");

                //获取硬件时间
                String yjTime = logs[9];

                //创建时间
                String year = yjTime.substring(0,4);
                String month = yjTime.substring(4,6);
                String day = yjTime.substring(6,8);
                String hour = yjTime.substring(8,10);
                String min = yjTime.substring(10,12);
                String second = yjTime.substring(12,14);

                String dt = year+"-"+month+"-"+day+" "+hour+":"+min+":"+second;

                Date cd = DateTool.string2Date(dt,"yyyy-MM-dd HH:mm:ss");


                Calendar cal = Calendar.getInstance();
                cal.setTime(cd);//date 换成已经已知的Date对象
                cal.add(Calendar.HOUR_OF_DAY, 8);// before 8 hour
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(format.format(cal.getTime()));

                cd = DateTool.string2Date(format.format(cal.getTime()),"yyyy-MM-dd HH:mm:ss");


                MessionLog messionLog = new MessionLog();

                messionLog.setCarNumber(logs[2]);
                messionLog.setRtcTime(logs[9]);
                messionLog.setGpsIsFail(0);
                messionLog.setGepUploadTime(logs[6]);
                messionLog.setLng(logs[7]);
                messionLog.setLat(logs[8]);
                messionLog.setCreateTime(cd);
                messionLog.setDoorMessage("close");

                messionLogDao.save(messionLog);

            }
            catch (Exception e){

                e.printStackTrace();

                socketLogSend.setCheckStatus(2);

                socketLogSendDao.save(socketLogSend);
            }
        }

//        messionLogDao.delete(messionLogsBefore);
    }
}
